package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QuerLeisureCardsInfoRes;

/**
 * Created by hyt on 2019/4/11.
 */
public class LeisureYearCardsDialogAdapter extends RecyclerView.Adapter<LeisureYearCardsDialogAdapter.LycCardHolder>{

    Context context;
    List<QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean> leisur_cards ;

    ILycardsAdatperCallBack iLycardsAdatperCallBack;

    boolean isShowTip = true;

    public LeisureYearCardsDialogAdapter(Context context, List<QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean> leisur_cards ){
        this.context = context;
        this.leisur_cards = leisur_cards;


        for(int i=0;i<leisur_cards.size();i++){
            leisur_cards.get(i).setFlag(false);
        }
    }


    @Override
    public LycCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_lycard_dialog, parent, false);
        return new LycCardHolder(view);
    }

    @Override
    public void onBindViewHolder(LycCardHolder holder, int position) {
        QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean leisur_cardsInfoRes = this.leisur_cards.get(position);

        holder.tv_cardNumber.setText(leisur_cardsInfoRes.getCardNo());

        if(leisur_cardsInfoRes.isFlag()){
            holder.iv_status.setImageResource(R.drawable.leisure_select);
        }else{
            holder.iv_status.setImageResource(R.drawable.leisure_unselect);
        }

        //         * acctType :1 线上开通
        //         * acctType :0 线下开通

        if(isShowTip){
            if(leisur_cardsInfoRes.getAcctType()==1){
                holder.tv_acctType.setText("在线办理套餐，确认后立即使用。");
            }else{
                holder.tv_acctType.setText("线下网点办理套餐，请上传实名认证本人真实并予于辨认的头像，次日生效。");
            }
        }else{
            holder.tv_acctType.setVisibility(View.GONE);
        }




        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(iLycardsAdatperCallBack!=null){

                    iLycardsAdatperCallBack.onBack(leisur_cardsInfoRes);
                }

                for(int i=0;i<leisur_cards.size();i++){
                    leisur_cards.get(i).setFlag(false);
                }

                leisur_cardsInfoRes.setFlag(!leisur_cardsInfoRes.isFlag());
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return leisur_cards!=null? leisur_cards.size():0;
    }


    public ILycardsAdatperCallBack getiLycardsAdatperCallBack() {
        return iLycardsAdatperCallBack;
    }

    public void setiLycardsAdatperCallBack(ILycardsAdatperCallBack iLycardsAdatperCallBack) {
        this.iLycardsAdatperCallBack = iLycardsAdatperCallBack;
    }

    public void showTip(boolean b) {
        isShowTip = b;
    }

    public static class LycCardHolder extends RecyclerView.ViewHolder {

        TextView tv_cardNumber;

        ImageView iv_status;

        TextView tv_acctType;

        public LycCardHolder(View itemView) {
            super(itemView);

            tv_cardNumber = itemView.findViewById(R.id.tv_cardNumber);
            iv_status = itemView.findViewById(R.id.iv_status);
            tv_acctType = itemView.findViewById(R.id.tv_acctType);
        }
    }

    public interface ILycardsAdatperCallBack{
        void onBack(QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean leisur_cardsInfoRes);
    }

}
